package com.kzj.common.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;

import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.kzj.common.properties.NoteProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 短信发送工具类
 */
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.core.http.HttpClient;
import com.aliyun.core.http.HttpMethod;
import com.aliyun.core.http.ProxyOptions;
import com.aliyun.httpcomponent.httpclient.ApacheAsyncHttpClientBuilder;
import com.aliyun.sdk.service.dysmsapi20170525.models.*;
import com.aliyun.sdk.service.dysmsapi20170525.*;
import com.google.gson.Gson;
import darabonba.core.RequestConfiguration;
import darabonba.core.client.ClientOverrideConfiguration;
import darabonba.core.utils.CommonUtil;
import darabonba.core.TeaPair;

//import javax.net.ssl.KeyManager;
//import javax.net.ssl.X509TrustManager;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@Slf4j
@Component
public class SMSUtils {
	@Autowired
	NoteProperties noteProperties;
	/**
	 * 发送短信
	 *
	 */
	public  void sendMessage(String phone,String code)
	{
		System.out.println(noteProperties);

		StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
				.accessKeyId(noteProperties.getAccessKeyId())
				.accessKeySecret(noteProperties.getAccessKeySecret())
				.build());
		AsyncClient client = AsyncClient.builder()
				.region(noteProperties.getRegionId()) // Region ID
				.credentialsProvider(provider)
				.overrideConfiguration(
						ClientOverrideConfiguration.create()
								.setEndpointOverride("dysmsapi.aliyuncs.com")
				)
				.build();

		SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
				.signName(noteProperties.getSignName())
				.templateCode(noteProperties.getTemplateCode())
				.phoneNumbers(phone)
				.templateParam("{\"code\":\""+code+"\"}")
				.build();
		CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
		SendSmsResponse resp = null;
		try {
			resp = response.get();
		} catch (InterruptedException e) {
			throw new RuntimeException(e);
		} catch (ExecutionException e) {
			throw new RuntimeException(e);
		}
		System.out.println(new Gson().toJson(resp));
		client.close();
	}
}
